<template>
  <div class="container">
    <!-- 头部 -->
    <div class="header">
      <div class="h-left">
        <div class="choose fs-32" v-show="!isChooseAll" @click="changeCheckAll()">全选</div>
        <div class="choose fs-32" v-show="isChooseAll" @click="changeCheckAll()">取消全选</div>
      </div>
      <div class="h-center">
        <div class="title fs-36">{{title}}</div>
      </div>
      <div class="h-right">
        <div class="close fs-32" @click="goBack">关闭</div>
      </div>
    </div>
    <!-- 歌曲列表部分 -->
    <div class="song-list">
      <div class="items" v-for="(item,index) in songList" :key="index">
        <div class="left">
          <div class="check-box" @click="chooseItem(item)">
            <img v-show="!item.checked" :src="check" alt />
            <img v-show="item.checked" :src="checked" alt />
          </div>
        </div>
        <div class="right">
          <div class="songName fs-36">{{item.music_name}}</div>
          <div class="singerName fs-26">
            <div class="img" v-if="item.isDownload">
              <img :src="downLoad" alt />
            </div>
            {{item.singer_name}}-{{item.music_list_name}}
          </div>
        </div>
      </div>
    </div>
    <!-- 底部操作列表部分 -->
    <div class="bottom">
      <div
        class="option-item"
        v-for="(item,index) in optionsList"
        :key="index"
        @click="optionCheck(item.id)"
      >
        <div class="img">
          <img :src="item.img" alt />
        </div>
        <div class="txt">{{item.txt}}</div>
      </div>
    </div>
    <!-- 收藏到歌单模态框 -->
    <div class="collectModel" @touchmove.prevent @click.stop="closeModel" v-show="isShow">
      <div class="add-box">
        <div class="add">
          <div class="img" @click.stop="addSheet">
            <img :src="add" alt />
          </div>
          <span class="text">新建歌单</span>
        </div>
        <div class="list">
          <div
            class="list-item"
            v-for="(item,index) in list"
            :key="index"
            @click="chooseSheet(item.song_sheet_id)"
          >
            <div class="center-col">
              <div class="left center-col">
                <img :src="item.song_sheet_photo" alt />
              </div>

              <div class="right flex-column">
                <p>{{item.song_sheet_name}}</p>
                <p class="color-9">{{item.music_num}}首</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 新建歌单 -->
    <div class="addmusic-list" @touchmove.prevent v-if="addshow">
      <div class="contents bg-f">
        <div class="top padding-24 space-between">
          <p class="fs-32 color-6" @click="cancel">取消</p>
          <p class="fs-32">新建歌单</p>
          <p class="fs-32 color-6" @click="sure">确定</p>
        </div>
        <div class="setmusic padding-24">
          <input type="text" placeholder="歌单名称" class="fs-32 color-6" v-model="songname" />
          <div class="center-col">
            <div class="yuan center bg-f" :class="{active:ac}" @click="checkStatus">
              <p></p>
            </div>
            <p class="fs-32 color-6">设置为隐私歌单</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "batch-operation",
  data() {
    return {
      store: this.getStore(),
      title: "批量操作",
      isChooseAll: false,
      check: require("@/assets/index/check.png"),
      checked: require("@/assets/index/checked.png"),
      downLoad: require("@/assets/index/selected.png"),
      add: require("../../assets/mine/add.png"),
      songList: [
        {
          id: 1,
          songName: "残酷月光",
          singerName: "林宥嘉",
          sheetName: "哈哈",
          checked: false,
          isDownload: true
        },
        {
          id: 2,
          songName: "飞云之下",
          singerName: "林俊杰",
          sheetName: "哈哈",
          checked: false,
          isDownload: true
        },
        {
          id: 3,
          songName: "出山",
          singerName: "花粥",
          sheetName: "哈哈",
          checked: false,
          isDownload: false
        },
        {
          id: 4,
          songName: "残酷月光",
          singerName: "林宥嘉",
          sheetName: "哈哈",
          checked: false,
          isDownload: false
        },
        {
          id: 5,
          songName: "残酷月光",
          singerName: "林宥嘉",
          sheetName: "哈哈",
          checked: false,
          isDownload: false
        }
      ], //歌曲列表数据
      optionsList: [
        {
          id: 1,
          img: require("@/assets/index/play-operation.png"),
          txt: "下一首播放"
        },
        {
          id: 2,
          img: require("@/assets/index/add-operation.png"),
          txt: "收藏到歌单"
        },
        {
          id: 3,
          img: require("@/assets/index/download-operation.png"),
          txt: "下载"
        },
        {
          id: 4,
          img: require("@/assets/index/delete-operation.png"),
          txt: "删除下载"
        }
      ], //底部操作选项列表
      playingList: [], //历史播放列表
      musicMes: [], //播放音乐信息
      result: null, //表示当前正在播放的音乐在播放列表里面位置
      checkedList: [], //选中要加到下一首播放的数组
      isShow: false, //添加到歌单的模态框  显示或隐藏
      addshow: false, //新增歌单
      songname: "", //新增的歌单名称
      userinfo: "", //用户信息
      ac: false, //控制是否是隐私歌单的样式
      list: [],
      musicIdList: [] //选中的音乐id数组
    };
  },
  components: {},
  methods: {
    // 获取用户信息
    getUserInfo() {
      var url = this.$host1 + `home/getPersonalMsg`;
      this.$axios
        .get(url)
        .then(res => {
          //console.log(res)
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.userinfo = resData.data;
              this.getUserSongSheetList();
            } else if (resData.code == 0) {
              // this.$router.push('/login');
            } else {
              this.AlertWin(resData.message);
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 获取用户的歌单列表
    getUserSongSheetList() {
      let url = this.$host1 + `home/userSongSheetList`;
      this.$axios
        .get(url, {
          params: {
            pageSize: 10000,
            page: 1,
            user_id: this.userinfo.user_id
          }
        })
        .then(res => {
          //console.log(res)
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.list = resData.data.list;
            } else {
              this.list = [];
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 选择歌单添加歌曲
    chooseSheet(id) {
      console.log("选择的是----" + id);
      console.log(this.checkedList, "---this.checkedList---");
      this.checkedList.map(item => {
        this.musicIdList.push(item.music_id);
      });
      this.addSongSheet(id);
    },
    // 批量添加到歌单
    addSongSheet(id) {
      const url = this.$host1 + `home/collectSomeSongSheet`;
      var params = {
        song_sheet_id: id,
        music_id: this.musicIdList
      };
      this.$axios
        .post(url, params)
        .then(res => {
          console.log(res.data, "---res.data--");
          if (res.data.code == 200) {
            this.AlertWin("添加成功");
            this.musicIdList = [];
          } else {
            this.musicIdList = [];
            this.AlertWin(res.data.message);
          }
        })
        .catch(err => {
          console.log(err, "err-----");
        });
    },
    // 切换是否设置为隐私歌单
    checkStatus() {
      this.ac = !this.ac;
    },
    // 确定新增歌单
    sure() {
      if (this.songname == "") {
        this.AlertWin("请输入歌单名");
        return;
      }
      var pe = 2;
      if (this.ac == true) {
        pe = 1;
      } else {
        pe = 2;
      }
      var url = this.$host1 + `home/addSongSheet`;
      var data = {
        song_sheet_name: this.songname,
        is_personal: pe
      };
      this.$axios
        .post(url, data)
        .then(res => {
          console.log(res);
          //return
          if (res.status == 200) {
            let resData = res.data;
            if (resData.code == 200) {
              this.addshow = false;
              this.songname = "";
              this.addshow = false;
              this.getUserSongSheetList(); //重新获取下用户歌单列表数据
              setTimeout(() => {
                this.AlertWin("添加成功");
              }, 500);
            } else {
              this.AlertWin(resData.message);
            }
          }
        })
        .catch(() => {
          this.AlertWin("网络错误！请稍后重试！");
        });
    },
    // 显示新增歌单模态框
    addSheet() {
      this.addshow = true;
    },
    // 取消新建歌单
    cancel() {
      this.songname = "";
      this.addshow = false;
    },
    // 关闭添加到歌单的模态框
    closeModel() {
      this.isShow = false;
    },
    // 选择或者取消选中
    chooseItem(item) {
      item.checked = !item.checked;
      this.$forceUpdate();
    },
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    // 改变全选和非全选状态
    changeCheckAll() {
      this.isChooseAll = !this.isChooseAll;
      this.songList.map(item => {
        if (this.isChooseAll) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
    },
    // 选择操作类型
    optionCheck(id) {
      if (id == 1) {
        // id == 1  点击了下一首播放
        this.checkedList = this.songList.filter(item => {
          return item.checked == true;
        });
        if (this.checkedList.length == 0) {
          this.AlertWin("请选择要播放的歌曲");
          return;
        }

        let number = JSON.parse(window.localStorage.getItem("playingList"))
          .length;
        if (number == 0) {
          console.log("-----"); //没有播放信息
          console.log(this.checkedList, "---this.checkedList");
          this.store.playingList = this.checkedList;
          window.localStorage.setItem(
            "playingList",
            JSON.stringify(this.checkedList)
          );
          this.AlertWin("添加成功");
        } else {
          this.playingList = JSON.parse(
            window.localStorage.getItem("playingList")
          );
          // console.log(this.playingList, "====this.playingList---before");
          // 先判断当前选中的歌曲是否已经存在于历史播放列表里面
          this.checkedList.map((item, index) => {
            if (item.music_id == musicId) {
              // 如果选择添加到下一首播放的歌曲正在播放   则从数组里移除这一项
              this.checkedList.splice(index, 1);
            }
          });
          // console.log(this.checkedList, "-this.checkedList--处理以后的");
          this.checkedList.map(item => {
            this.playingList.map((res, num) => {
              // 如果要添加的歌曲已经在播放列表里面则先删除调播放列表里面的，然后再添加进去
              if (res.music_id == item.music_id) {
                this.playingList.splice(num, 1);
              }
            });
          });
          this.musicMes = JSON.parse(window.localStorage.getItem("musicMes"));
          let musicId = this.musicMes.music_id; //表示本地返回的正在播放音乐的id，
          // console.log(musicId,'--musicId');
          this.playingList.map((item, index) => {
            if (item.music_id == musicId) {
              this.result = index + 1; //取出当前播放音乐在播放列表的下标   加一表示位置
            }
          });
          // console.log(this.playingList, "====this.playingList--after");
          if (this.result == this.playingList.length) {
            // 表明当前正在播放的歌曲位于数组的最后一位
            this.checkedList.map(item => {
              this.playingList.unshift(item);
            });
          } else {
            this.checkedList.map(item => {
              this.playingList.splice(this.result, 0, item);
            });
          }
          this.AlertWin("添加成功");
          console.log(this.playingList, "---this.playingList");
          this.store.playingList = this.playingList;
          window.localStorage.setItem(
            "playingList",
            JSON.stringify(this.playingList)
          );
        }
      } else if (id == 2) {
        // 表示点击了 收藏到歌单
        this.checkedList = this.songList.filter(item => {
          return item.checked == true;
        });
        if (this.checkedList.length == 0) {
          this.AlertWin("请选择要收藏的歌曲");
          return;
        }
        this.isShow = true;
      }
    }
  },
  created() {
    this.songList = this.$route.query.list;
    if (this.songList && this.songList.length != 0) {
      this.songList.map(item => {
        item.checked = false;
      });
    }
    this.getUserInfo();
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  min-height: 100vh;
  padding-top: 1rem;
  box-sizing: border-box;
  background-color: #fff;
  .header {
    width: 100%;
    height: 1rem;
    padding: 0 0.34rem 0 0.31rem;
    box-sizing: border-box;
    background: linear-gradient(to right, #680dae, #9529c6);
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .h-left {
      .choose {
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
    .h-center {
      .title {
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
    .h-right {
      .close {
        font-weight: 500;
        color: rgba(255, 255, 255, 1);
        line-height: 0.32rem;
      }
    }
  }

  .song-list {
    padding: 0 0.28rem;
    box-sizing: border-box;
    .items {
      padding: 0.41rem 0 0.38rem 0;
      box-sizing: border-box;
      border-bottom: 0.01rem solid #e9e9e9;
      display: flex;
      .left {
        margin-top: 0.15rem;
        margin-right: 0.21rem;
        .check-box {
          width: 0.56rem;
          height: 0.56rem;
          img {
            width: 100%;
          }
        }
      }
      .right {
        .songName {
          font-weight: 500;
          color: rgba(68, 68, 68, 1);
          line-height: 0.36rem;
          margin-bottom: 0.22rem;
        }
        .singerName {
          display: flex;
          font-weight: 500;
          color: rgba(153, 153, 153, 1);
          line-height: 0.36rem;
          align-items: center;
          .img {
            width: 0.3rem;
            height: 0.3rem;
            margin-right: 0.11rem;
            img {
              width: 100%;
            }
          }
        }
      }
    }
  }

  .bottom {
    width: 100%;
    height: 1.2rem;
    position: fixed;
    left: 0;
    bottom: 0;
    background: rgba(248, 248, 248, 1);
    display: flex;
    justify-content: space-around;
    align-items: center;
    .option-item {
      display: flex;
      flex-direction: column;
      .img {
        width: 0.46rem;
        height: 0.46rem;
        margin: 0 auto;
        margin-bottom: 0.16rem;
        img {
          width: 100%;
        }
      }
      .txt {
        text-align: center;
      }
    }
  }

  // 收藏到歌单模态框
  .collectModel {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    .add-box {
      position: absolute;
      width: 90%;
      // height: 5rem;
      background-color: #fff;
      left: 50%;
      top: 25%;
      border-radius: 0.14rem;
      transform: translate(-50%);
      padding: 0.24rem;
      box-sizing: border-box;
      .add {
        display: flex;
        align-items: center;
        margin-bottom: 0.24rem;
        .img {
          position: relative;
          width: 1rem;
          height: 1rem;
          margin-right: 0.25rem;
          background-color: rgba(248, 248, 248, 1);
          img {
            width: 0.3rem;
            height: 0.3rem;
            position: absolute;
            left: 50%;
            top: 35%;
            transform: translate(-50%);
          }
          span {
          }
        }
      }

      .list {
        width: 100%;
        height: auto;
        .list-item {
          height: 1rem;
          margin-bottom: 0.12rem;
          padding-bottom: 0.12rem;
          box-sizing: border-box;
          .center-col {
            width: 100%;
            height: 100%;
            .left {
              width: auto;
              min-width: 1rem;
              max-width: 1.5rem;
              .yuan {
                width: 0.36rem;
                height: 0.36rem;
                margin-right: 0.2rem;
                p {
                  width: 0.2rem;
                  height: 0.03rem;
                }
              }
              img {
                width: 0.88rem;
                height: 0.88rem;
              }
            }
            .right {
              margin-left: 0.2rem;
              height: 0.88rem;
            }
          }
        }
      }
    }
  }

  //新建歌单模态框
  .addmusic-list {
    position: fixed;
    background-color: rgba(0, 0, 0, 0.8);
    z-index: 99999;
    width: 100%;
    height: 100vh;
    top: 0;
    left: 0;
    .contents {
      width: 100%;
      height: 5.3rem;
      position: absolute;
      bottom: 0;
      left: 0;
      border-radius: 0.15rem 0.15rem 0 0;
      .top {
        height: 1rem;
        border-bottom: 0.01rem solid #e9e9e9;
      }
      .setmusic {
        padding: 0.2rem 0.24rem;
        input {
          width: 100%;
          height: 0.6rem;
          margin-top: 0.5rem;
          border: 0.015rem solid #999;
          border-radius: 0.12rem;
          text-indent: 0.2rem;
          //background-color: greenyellow;
        }
        .center-col {
          width: 100%;
          height: 0.6rem;
          margin-top: 0.2rem;
          //background-color: greenyellow;
          .yuan {
            width: 0.4rem;
            height: 0.4rem;
            margin-right: 0.2rem;
            border: 0.01rem solid #e9e9e9;
            p {
              width: 0.3rem;
              height: 0.3rem;
              border-radius: 50%;
              background-color: #fff;
            }
          }
          .active {
            border: 0.01rem solid #f44349;
            p {
              background-color: #f44349;
            }
          }
        }
      }
    }
  }
}
</style>
